﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OBUActivation
{
    /// <summary>
    /// 工具类
    /// </summary>
    class SamUtil
    {
        /// <summary>
        /// 将短整形转换为字节数组
        /// </summary>
        /// <param name="u">短整形</param>
        /// <returns>字节数组</returns>
        public static byte[] ConvertShort2Array(ushort u)
        {
            byte[] result = new byte[2];
            result[0] = (byte)((u >> 8 * 1) & 0X00FF);
            result[1] = (byte)((u >> 8 * 0) & 0X00FF);
            return result;
        }

        /// <summary>
        /// 将整形转换为字节数组
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static byte[] ConvertInt2Array(uint i)
        {
            byte[] result = new byte[4];
            result[0] = (byte)((i >> 8 * 3) & 0x000000FF);
            result[1] = (byte)((i >> 8 * 2) & 0x000000FF);
            result[2] = (byte)((i >> 8 * 1) & 0x000000FF);
            result[3] = (byte)((i >> 8 * 0) & 0x000000FF);
            return result;
        }

        /// <summary>
        /// 将长整形转换为字节数组
        /// </summary>
        /// <param name="l">长整形</param>
        /// <returns>字节数组</returns>
        public static byte[] ConvertLong2Array(ulong l)
        {
            byte[] result = new byte[8];
            result[0] = (byte)((l >> 8 * 7) & 0X00000000000000FF);
            result[1] = (byte)((l >> 8 * 6) & 0X00000000000000FF);
            result[2] = (byte)((l >> 8 * 5) & 0X00000000000000FF);
            result[3] = (byte)((l >> 8 * 4) & 0X00000000000000FF);
            result[0] = (byte)((l >> 8 * 3) & 0X00000000000000FF);
            result[1] = (byte)((l >> 8 * 2) & 0X00000000000000FF);
            result[2] = (byte)((l >> 8 * 1) & 0X00000000000000FF);
            result[3] = (byte)((l >> 8 * 0) & 0X00000000000000FF);
            return result;
        }

        /// <summary>
        /// 将字符串转换为数组
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static byte[] ConvertString2Array(String s)
        {
            byte[] result = Encoding.Default.GetBytes(s);
            return result;
        }

        /// <summary>
        /// 将字符串转化为数组
        /// </summary>
        /// <param name="s">字符串</param>
        /// <param name="l">转化长度</param>
        /// <returns>字节数组</returns>
        public static byte[] ConvertString2Array(string s, int l)
        {
            byte[] result = new byte[l];
            byte[] temp = Encoding.Default.GetBytes(s);
            Array.Copy(temp, result, temp.Length);
            return result;
        }

        /// <summary>
        /// 将字节数组转化为字符串
        /// </summary>
        /// <param name="b">字节数组</param>
        /// <returns>字符串</returns>
        public static string ConvertArray2String(byte[] b)
        {
            string result = Encoding.Default.GetString(b);
            return result;
        }

        /// <summary>
        /// 将字节数组转化为字符串
        /// </summary>
        /// <param name="b">字节数组</param>
        /// <param name="s">起始位置</param>
        /// <param name="l">转化长度</param>
        /// <returns>字符串</returns>
        public static string ConvertArray2String(byte[] b, int s, int l)
        {
            string result = Encoding.Default.GetString(b, s, l);
            return result;
        }

        /// <summary>
        /// 将十六进制字符串转化为十六进制字节数组
        /// </summary>
        /// <param name="s">十六进制字符串</param>
        /// <returns>十六进制字节数组</returns>
        public static byte[] ConvertHexString2Array(string s)
        {
            byte[] result = new byte[s.Length / 2];
            for (int i = 0; i < s.Length / 2; i++)
            {
                result[i] = Convert.ToByte("0X" + s.Substring(2 * i, 2), 16);
            }
            return result;
        }

        /// <summary>
        /// 将十六进制字符串转化为十六进制字节数组
        /// </summary>
        /// <param name="s">十六进制字符串</param>
        /// <param name="l">转化长度</param>
        /// <returns>十六进制字节数组</returns>
        public static byte[] ConvertHexString2Array(string s, int l)
        {
            if (l % 2 != 0 || l > s.Length)
            {
                l = s.Length;
            }
            byte[] result = new byte[l / 2];
            for (int i = 0; i < l / 2; i++)
            {
                result[i] = Convert.ToByte("0X" + s.Substring(2 * i, 2), 16);
            }
            return result;
        }

        /// <summary>
        /// 将十六进制字节数组转化为十六进制字符串
        /// </summary>
        /// <param name="b">十六进制字节数组</param>
        /// <returns>十六进制字符串</returns>
        public static string ConvertArray2HexString(byte[] b)
        {
            string result = "";
            for (int i = 0; i < b.Length; i++)
            {
                result += b[i].ToString("X2");
            }
            return result;
        }

        /// <summary>
        /// 将十六进制字节数组转化为十六进制字符串
        /// </summary>
        /// <param name="b">十六进制字节数组</param>
        /// <param name="l">转化长度</param>
        /// <returns>十六进制字符串</returns>
        public static string ConvertArray2HexString(byte[] b, int l)
        {
            string result = "";
            if (l > b.Length)
            {
                l = b.Length;
            }
            for (int i = 0; i < l; i++)
            {
                result += b[i].ToString("X2");
            }
            return result;
        }
    }
}
